package com.atguigu.educms.service.impl;

import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.entity.vo.BannerQuery;
import com.atguigu.educms.mapper.CrmBannerMapper;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-04
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    public void pageQuery(Page<CrmBanner> crmBannerPage, BannerQuery bannerQuery) {
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        if (bannerQuery != null) {
            String name = bannerQuery.getName();
            String begin = bannerQuery.getBegin();
            String end = bannerQuery.getEnd();

            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like("title", name);
            }
            if (!StringUtils.isEmpty(begin)) {
                queryWrapper.ge("gmt_create", begin);
            }
            if (!StringUtils.isEmpty(end)) {
                queryWrapper.le("gmt_modified", end);
            }
        }
        //排序
        queryWrapper.orderByDesc("gmt_create");

        baseMapper.selectPage(crmBannerPage, queryWrapper);
    }

    @Override
    @Cacheable(value = "banner", key="'selectIndexList'")
    public List<CrmBanner> selectAllBanner() {
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 4");
        List<CrmBanner> list = list(queryWrapper);
        return list;
    }
}
